//
//  Sp_MemberStorage.m
//  liangyi
//
//  Created by 张水 on 16/12/28.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "Sp_MemberStorage.h"
#import "Sp_Member.h"
@implementation Sp_MemberStorage

+ (void)insertMemberWithUserModel:(UserModel *)model {
    [self deleteAllMembers];
    
    Sp_Member *member = [Sp_Member MR_createEntity];
    member.status = model.status;
    member.userLevel = model.userLevel;
    member.userPhone = model.userPhone;
    member.userName = model.userName;
    member.userScore = model.userScore;
    member.uid = model.id;
    member.userHead = model.userHead;
    member.createTime = model.createTime;
    member.userAge = model.userAge;
    
    
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

+ (void)updateMemberWithUserModel:(UserModel *)model {
    
    NSArray *arr = [Sp_Member MR_findByAttribute:@"uid" withValue:model.id];
    if (arr.count == 1) {
        
        Sp_Member *member = arr[0];
        member.status = model.status;
        member.userLevel = model.userLevel;
        member.userPhone = model.userPhone;
        member.userName = model.userName;
        member.userScore = model.userScore;
        member.uid = model.id;
        member.userHead = model.userHead;
        member.createTime = model.createTime;
        member.userAge = model.userAge;
        
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        
    }else{
        assert(@"find result not only one");
    }
    
}

+ (UserModel *)findMember:(NSString *)userid {
    
    NSArray *arr = [Sp_Member MR_findByAttribute:@"uid" withValue:userid];
    
    UserModel *model = [UserModel new];
    if (arr.count == 1) {
        Sp_Member *mem = arr[0];
        model = [UserModel new];
        model.status = mem.status;
        model.userLevel = mem.userLevel;
        model.userPhone = mem.userPhone;
        model.userName = mem.userName;
        model.userScore = mem.userScore;
        model.id = mem.uid;
        model.userHead = mem.userHead;
        model.createTime = mem.createTime;
        model.userAge =  mem.userAge;
    }
    return model;
}

+ (BOOL)deleteAllMembers {
    BOOL s = [Sp_Member MR_truncateAll];
    
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
    return s;
}


@end
